package com.runa.rccprotocol.http;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.runa.rccprotocol.config.RestTemplateConfig;
import com.runa.rccprotocol.flightControl.ControlTask;
import com.runa.rccprotocol.flightControl.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * 2024/9/6 10:12
 * zhangYB
 */
@Slf4j
@Service
public class HttpService {


    @Resource
    private RestTemplate restTemplate;


    public <T> ResponseEntity<T> post(String url, Object request, Class<T> responseType, String authorization) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization", authorization);
        HttpEntity httpEntity = new HttpEntity(request, httpHeaders);
        return post(url, httpEntity, responseType);
    }

    public <T> ResponseEntity<T> post(String url, Object httpEntity, Class<T> responseType) {
        ResponseEntity<T> responseEntity = null;
        try {
            responseEntity = restTemplate.postForEntity(url, httpEntity, responseType);
        } catch (Exception e) {
            log.error("", e);
        }
        return responseEntity;
    }

    public <T> ResponseEntity<T> put(String url, Object request, Class<T> responseType, String authorization) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization", authorization);
        HttpEntity httpEntity = new HttpEntity(request, httpHeaders);
        return put(url, httpEntity, responseType);
    }

    public <T> ResponseEntity<T> put(String url, HttpEntity httpEntity, Class<T> responseType) {
        return exchange(url, HttpMethod.PUT, httpEntity, responseType);
    }

    public static void main(String[] args) {
        String s = "{\"holdInfo\":\"hello\",\"httpMethod\":\"PUT\",\"priority\":83,\"request\":{\"controlCycle\":\"3\",\"controlOutTemperature\":\"20\",\"terminalValvesBatchControlParameters\":[{\"buildingNumber\":\"006号楼\",\"houseNumber\":\"0501室\",\"householdNumber\":\"\",\"residentialQuartersName\":\"安子A3小区\",\"unitNumber\":\"01单元\",\"valveAddress\":\"04333048\"}],\"type\":\"开启自控\"},\"responseType\":\"com.runa.rccprotocol.flightControl.Response\",\"task\":\"velit\",\"url\":\"http://199.28.10.137:8081/api/mock/100002/fk/temp\"}";
        ControlTaskTest(s);
        s = "{\"holdInfo\":\"hello\",\"httpMethod\":\"PUT\",\"priority\":94,\"request\":{\"openingControlValue\":\"1\",\"terminalValvesBatchControlParameters\":[{\"buildingNumber\":\"13\",\"houseNumber\":\"101\",\"householdNumber\":\"110101010\",\"residentialQuartersName\":\"华侨城-纯木岸\",\"unitNumber\":\"1\",\"valveAddress\":\"10370102113702\"}]},\"responseType\":\"com.runa.rccprotocol.flightControl.Response\",\"task\":\"nostrud\",\"url\":\"http://199.28.10.137:8081/api/mock/100002/fk/valve\"}";
        ControlTaskTest(s);

    }

    public static void ControlTaskTest(String s) {
        ControlTask controlTask = JSON.parseObject(s, ControlTask.class);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization", "Bearer a693b026-a2f4-416f-9d81-a6afb63f931b");
        HttpEntity httpEntity = new HttpEntity(controlTask.getRequest(), httpHeaders);
        ResponseEntity<Response> responseEntity = null;
        try {
            System.out.println(controlTask.getResponseType());
            RestTemplateConfig restTemplateConfig = new RestTemplateConfig();
            RestTemplate restTemplate = restTemplateConfig.restTemplate();
            responseEntity = restTemplate.exchange(controlTask.getUrl(), controlTask.getHttpMethod(), httpEntity, controlTask.getResponseType());
            System.out.println(JSON.toJSONString(responseEntity));
            System.out.println(responseEntity.getBody());
        } catch (Exception e) {
            log.error("", e);
        }
    }

    public <T> ResponseEntity<T> exchange(String url, HttpMethod httpMethod, Object request, Class<T> responseType, String authorization) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization", authorization);
        HttpEntity httpEntity = new HttpEntity(request, httpHeaders);
        return exchange(url, httpMethod, httpEntity, responseType);
    }

    public <T> ResponseEntity<T> exchange(String url, HttpMethod httpMethod, HttpEntity httpEntity, Class<T> responseType) {
        ResponseEntity<T> responseEntity = null;
        try {
            responseEntity = restTemplate.exchange(url, httpMethod, httpEntity, responseType);
        } catch (Exception e) {
            log.error("url: {}, httpMethod: {}, httpEntity: {}, responseType: {} "
                    , url, httpMethod, JSON.toJSONString(httpEntity), responseType.getName(), e);
        }
        return responseEntity;
    }




}
